<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>闭包陷阱</title>
    </head>
    <body>
        <button id="btn">add</button>
        <script>
            // function foo() {
            //     var el = document.getElementById("btn");
            //     el.onclick = function () {
            //         console.log(el.id);
            //     };
            // }
            // foo();

            function foo() {
                var el = document.getElementById("btn");
                el.onclick = function () {
                    console.log(el.id);
                    el = null;
                };
            }
            foo();
        </script>
    </body>
</html>
